package com.punai.customer.controller;

import cn.hutool.core.util.StrUtil;
import com.punai.common.annotation.Log;
import com.punai.common.core.controller.BaseController;
import com.punai.common.core.domain.AjaxResult;
import com.punai.common.core.page.TableDataInfo;
import com.punai.common.enums.BusinessType;
import com.punai.common.utils.StringUtils;
import com.punai.customer.domain.CusGxFee;
import com.punai.customer.domain.CusLinkFeature;
import com.punai.customer.domain.CusVersionsDiscrepancy;
import com.punai.customer.domain.contants.CusConstants;
import com.punai.customer.domain.request.CusRewriteCodeRequest;
import com.punai.customer.domain.vo.GxFeeVo;
import com.punai.customer.service.ICusGxFeeService;
import com.punai.customer.service.ICusVersionsDiscrepancyService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

/**
 * 工序分包费用项Controller
 * 
 * @author zxw
 * @date 2022-12-09
 */
@RestController
@RequestMapping("/cus/gxFee")
public class CusGxFeeController extends BaseController
{
    @Autowired
    private ICusGxFeeService dmcGxFeeService;
    @Autowired
    private ICusVersionsDiscrepancyService cusVersionsDiscrepancyService;

    /**
     * 查询工序分包费用项列表
     */
    @PreAuthorize("@ss.hasPermi('cus:gxFee:list')")
    @GetMapping("/list")
    public TableDataInfo list(CusGxFee dmcGxFee)
    {
        if (StrUtil.isEmpty(dmcGxFee.getChapterId())) {
            return new TableDataInfo();
        }
        return dmcGxFeeService.selectDmcGxFeeList(dmcGxFee,getCompanyId());
    }

    /**
     * 获取工序分包费用项详细信息
     */
    @PreAuthorize("@ss.hasPermi('cus:gxFee:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") String id)
    {
        return AjaxResult.success(dmcGxFeeService.selectDmcGxFeeById(id));
    }

    @PreAuthorize("@ss.hasPermi('cus:gxFee:query')")
    @GetMapping(value = "/getGxFeeInfo/{id}")
    public AjaxResult getGxFeeInfo(@PathVariable("id") String id)
    {
        return AjaxResult.success(dmcGxFeeService.getGxFeeInfo(id,getCompanyId()));
    }


    /**
     * 新增工序分包费用项
     */
    @PreAuthorize("@ss.hasPermi('cus:gxFee:add')")
    @Log(title = CusConstants.GX_NAME, businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody CusGxFee dmcGxFee)
    {
        return toAjax(dmcGxFeeService.insertDmcGxFee(dmcGxFee));
    }

    /**
     * 修改工序分包费用项
     */
    @PreAuthorize("@ss.hasPermi('cus:gxFee:edit')")
    @Log(title = CusConstants.GX_NAME, businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody GxFeeVo vo)
    {
        return toAjax(dmcGxFeeService.updateDmcGxFee(vo));
    }

    /**
     * 删除工序分包费用项
     */
    @PreAuthorize("@ss.hasPermi('cus:gxFee:remove')")
    @Log(title = CusConstants.GX_NAME, businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable String[] ids)
    {
        return toAjax(dmcGxFeeService.deleteDmcGxFeeByIds(ids));
    }

    @GetMapping( "/getCodeLayout/{specId}")
    public AjaxResult getCodeLayout(@PathVariable String specId){
        return AjaxResult.success(dmcGxFeeService.getCodeLayout(specId));
    }

    @PreAuthorize("@ss.hasPermi('cus:gxFee:rewriteCode')")
    @Log(title = CusConstants.GX_NAME + "-重新编码", businessType = BusinessType.UPDATE)
    @PostMapping("/rewriteCode")
    public AjaxResult rewriteCode(@RequestBody CusRewriteCodeRequest codeRequest)
    {
        return toAjax(dmcGxFeeService.rewriteCode(codeRequest));
    }

    @PreAuthorize("@ss.hasPermi('cus:gxFee:publish')")
    @GetMapping( "/validateCode/{specId}")
    public AjaxResult validateCode(@PathVariable String specId)
    {
        if (StringUtils.isEmpty(specId) || StringUtils.equals(specId,"null")) {
            return AjaxResult.error("专业获取失败");
        }
        return AjaxResult.success(dmcGxFeeService.validateCode(getCompanyId(),specId));
    }

    @GetMapping("/getContrastData")
    public TableDataInfo getDataLog(CusVersionsDiscrepancy cusVersionsDiscrepancy) {
        startPage();
        cusVersionsDiscrepancy.setCompanyId(getCompanyId());
        cusVersionsDiscrepancy.setPublishType(CusConstants.GX_TYPE);
        return getDataTable(cusVersionsDiscrepancyService.selectStaVersionsDiscrepancyList(cusVersionsDiscrepancy));
    }

    @GetMapping(value = "/getStaGxFee/{specId}/{id}")
    public AjaxResult getStaMaterialItem(@PathVariable("specId") String specId,@PathVariable("id") String id)
    {
        return AjaxResult.success(dmcGxFeeService.getStaGxFeeById(id,specId,getCompanyId()));
    }

    @PreAuthorize("@ss.hasPermi('cus:gxFee:recoverLinkCategory')")
    @Log(title = CusConstants.GX_NAME + "-恢复关联类别", businessType = BusinessType.UPDATE)
    @PutMapping(value = "/recoverLinkCategory")
    public TableDataInfo recoverLinkCategory(@RequestBody CusGxFee gxFee)
    {
        return getDataTable(dmcGxFeeService.recoverLinkCategory(gxFee.getId(),getCompanyId()));
    }

    /**
     * 获取标准费用项关联材机类别
     */
    @GetMapping(value = "/getGxLinkCategory/{id}")
    public AjaxResult getGxLinkCategory(@PathVariable("id") String id)
    {
        return AjaxResult.success(dmcGxFeeService.getGxLinkCategory(id,getCompanyId()));
    }

    @Log(title = CusConstants.GX_NAME + "-移动费用项", businessType = BusinessType.UPDATE)
    @PutMapping("/moveToChapter/{ids}/{chapterId}")
    public AjaxResult moveToChapter(@PathVariable String[] ids,@PathVariable String chapterId)
    {
        return toAjax(dmcGxFeeService.moveToChapter(ids,chapterId));
    }

    @PreAuthorize("@ss.hasPermi('cus:gxFee:delLinkFeature')")
    @PutMapping("/checkFeatureDescribe")
    public AjaxResult checkFeatureDescribe(@RequestBody CusLinkFeature cusLinkFeature)
    {
        String itemId = cusLinkFeature.getItemId();
        String featureId = cusLinkFeature.getFeatureId();
        if (StringUtils.isEmpty(itemId)) {
            return AjaxResult.error("费用项id获取失败");
        }
        if (StringUtils.isEmpty(featureId)) {
            return AjaxResult.error("特征id获取失败");
        }
        return AjaxResult.success(dmcGxFeeService.checkFeatureDescribe(itemId,featureId));
    }
}
